ALTER FUNCTION DBO.FORMATDOCUMENT(@VALUE SYSNAME, @TYPE CHAR(1)) 
RETURNS SYSNAME
AS
BEGIN
	DECLARE @RETURN SYSNAME
	IF @TYPE = 'F' 
	BEGIN
		SELECT @RETURN = STUFF( STUFF( STUFF( @VALUE, 4, 0, '.'), 8, 0, '.'), 12, 0, '-')
	END
	ELSE
	IF @TYPE = 'J' 
	BEGIN
		SELECT @RETURN = STUFF( STUFF( STUFF( STUFF( @VALUE, 3, 0, '.'), 7, 0, '.'), 11, 0, '/'), 16, 0, '-')
	END
	RETURN @RETURN
END
GO

select stuff( stuff( stuff( stuff( '63214878000140', 3, 0, '.'), 7, 0, '.'), 11, 0, '.'), 16, 0, '-')


select dbo.formatdocument('50513230572', 'F')
select dbo.formatdocument('50513230572', 'J')
select dbo.formatdocument('505132305000172', 'J')
select dbo.formatdocument('505132305000172', 'F')
select dbo.formatdocument('63214878000140', 'J')

select top 100 pfi_cpf,  dbo.formatdocument( pfi_cpf , 'f'), * from ger_pessoa_fisica where pfi_cpf is not null
select top 100 PJU_CNJP, dbo.formatdocument( PJU_CNJP, 'J'), * from ger_pessoa_juridica where PJU_CNJP is not null